package com.example.jsondemo;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements FetchQuestionsListener {

	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		// show progress dialog
		dialog = ProgressDialog.show(this, "", "Loading...");

		String url = "http://10.0.2.2/quiz/jsondemo.php";
		FetchQuestions task = new FetchQuestions(this);
		task.execute(url);
	}

	@Override
	public void onFetchFailure(String msg) {
		// dismiss the progress dialog
		if (dialog != null)
			dialog.dismiss();
		// show failure message
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onFetchComplete(List<Questions> data) {
		// dismiss the progress dialog
		if (dialog != null)
			dialog.dismiss();
		// create new adapter
		QuestionsAdapter adapter = new QuestionsAdapter(this, data);
		// set the adapter to list
		setListAdapter(adapter);
	}
}